<?php
/**
 *      add by yangyong       coding on 2017-08-19 
 */
    loadcore();
    $return = array();
    $fid = intval($_GET['fid']);
    $subject = $_GET['subject'];
    $message = $_GET['message'];
    $authorid = intval($_GET['authorid']);
    if(empty($fid) || empty($subject) || empty($message) || empty($authorid) ){
        $return['code'] = '1';
        $return['msg'] = '缺少主要参数！';
        echo json_encode($return);exit;
    }
    $forum_info = C::t('forum_forum')->fetch_info_by_fid($fid);
    $authorinfo = getuserbyuid($authorid);
    if(empty($forum_info) || empty($authorinfo)){
        $return['code'] = '1';
        $return['msg'] = '缺少主要参数！';
        echo json_encode($return);exit;
    }
    $tid = addThread($fid,$subject,$message,$authorid);
    if($tid > 0){
        $return['code'] = '0';
    }else{
        $return['code'] = '1';
        $return['msg'] = '发帖失败！';
    }
    echo json_encode($return);exit;
    /*
    * 添加帖子
    * $fid          要发表到的板块ID
    * $subject      帖子的标题
    * $message      帖子的内容
    * $authorid     帖子的作者id    也就是对应到的论坛的uid
    */
    function addThread($fid,$subject,$message,$authorid){
        $members = getuserbyuid($authorid);
        $author = $members['username'];
        // $fid = $data['fid']; subject = $data['subject']; message =
        // $$data['message']; authorid = $data['authorid']; author =
        // $$data['author'];w
        $proc_time = time();
        $thread = $post = array();

        $thread['fid'] = $fid;//板块ID
        $thread['subject'] = $subject;//标题
        $thread["authorid"]= $authorid;
        $thread["author"]= $author;
        $thread["dateline"]= $proc_time;
        $thread["lastpost"]= $proc_time;
        $thread["lastposter"]= $author;
        //插件必须添加special参数，否则无法按照插件的样式进行显示
        $thread["special"]= 0;//特殊主题,1:投票;2:商品;3:悬赏;4:活动;5:辩论贴;127:插件相关
        
        $tid=C::t('forum_thread')->insert($thread, 1);//添加到帖子列表
        
        if($tid){
            $post["tid"] = $tid;
            $post['fid'] = $fid;
            $post["position"]= 1;
            $post["smileyoff"]= "-1";
            $post["bbcodeoff"]= "-1";
            $post["usesig"]= 1;
            $post["author"]= $author;
            $post["authorid"]= $authorid;
            $post["subject"]= $subject;
            $post["message"]= $message;
            $post["dateline"]= $proc_time;
            $post["first"]= 1;

            $pid = C::t('forum_post_tableid')->insert(array('pid' => null), true);//添加pre_forum_post表的pid最大值
            $post["pid"]= $pid;
            $okid=C::t('forum_post')->insert("0", $post, 1);//写入帖子
            
            $lastpost = $tid . "$subject" . $proc_time . $author;
            $sql = "update " . DB::table('forum_forum') . " set threads = threads + 1, todayposts = todayposts + 1, lastpost = '$lastpost' where fid= $fid";//修改今日主题和帖子数量
            DB::query($sql);
        }

        return $tid;
    }



?>